﻿using System;
using System.Diagnostics;
using System.Windows.Forms;
using OutlookContactsSync.Domain.Presenters;
using OutlookContactsSync.Domain.Properties;
using OutlookContactsSync.Domain.Views;
using OutlookContactsSync.Presenters;
using System.Threading;

namespace OutlookContactsSync
{
    public sealed partial class MainForm : BaseForm, IOutlookSyncMainView   
    {
        public MainForm()
        {
            InitializeComponent();

            //Start sync
            ContactPicture = Resources.syncing;
            Syncing = true;            
        }

        public override void ShowForm()
        {            
            titleLabel.Text = Resources.SynchronizeOutlookContacts;
            ContactPicture = Resources.UnknownPicture;
                        
            this.ShowDialog();            
        }

        #region IOutlookSyncMainView Members

        public event EventHandler StartSync;
        public event EventHandler CancelSync;

        public string ProgressMessage
        {
            set { progressLabel.Text = value; }
        }

        public string CurrentContactName
        {
            set { contactNameLabel.Text = value; }
        }

        public int Progress
        {
            set { progressBar.Value = value; }
        }

        public string ProgressCounter
        {
            set
            {
                labelProgressCounter.Text = value;
            }
        }

        public bool ShowProgressCounter
        {
            set
            {
                labelProgressCounter.Visible = value;
            }
        }

        public bool Syncing
        {
            set
            {
                resultsButton.Enabled = !value;                
                cancelButton.Enabled = value;
            }
        }

        public System.Drawing.Image ContactPicture
        {
            set
            {
                contactPictureBox.Image = value;
            }
            get
            {
                return contactPictureBox.Image;
            }
        }

        #endregion

        private void ResultsButtonClick(object sender, EventArgs e)
        {
            this.Hide();
            IOutlookSyncStatusPresenter outlookSynStatusPresenter = new OutlookSyncStatusPresenter(new StatusForm());
            outlookSynStatusPresenter.InitView();          
        }        

        private void CancelButtonClick(object sender, EventArgs e)
        {
            if (CancelSync != null)
            {
                CancelSync(this, EventArgs.Empty);
            }
        }        

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Trace.WriteLine("App closed", "info");
        }

        public void SyncSuccesFullyFinished()
        {            
            Syncing = false;                     
        }
    }
}